export function moveArrayBy(array, sourceValue, targetValue, key, addIndex = 0) {
  let newArray = [...array]
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      if (item[key] == sourceValue) {
        sourceParent = arr;
        sourceIndex = i;
      } else if (item[key] == targetValue) {

        targetParent = arr;
        targetIndex = i;
      }
      if (!sourceParent || !targetParent) {
        if (Array.isArray(arr[i].children)) {
          findItem(arr[i].children);
        }
      }
      if (sourceParent && targetParent) {
        break;
      }
    }
  }
  let sourceParent = null;
  let sourceIndex = null;
  let targetParent = null;
  let targetIndex = null;
  findItem(newArray);
  // debugger
  if (sourceParent && targetParent) {
    let temp = { ...sourceParent[sourceIndex] };
    const insertIndex = targetIndex + addIndex;
    if (targetParent == sourceParent) {
      targetParent.splice(insertIndex + 1, 0, temp)
      // sourceParent.splice(sourceIndex, 1)
      if (targetIndex > sourceIndex) {
        sourceParent.splice(sourceIndex, 1)
      } else {
        sourceParent.splice(sourceIndex + 1, 1)
      }
    } else {
      targetParent.splice(insertIndex + 1, 0, temp)
      sourceParent.splice(sourceIndex, 1)
    }
    if (targetParent.length > 1) {

      const index = targetParent.findIndex(v => v.component === 'Placeholder')
      if (index > -1) {
        targetParent.splice(index, 1)
      }
    }
    // if (insertIndex == -1) {
    //   targetParent.unshift(temp);
    // } else {
    //   if (sourceIndex <= insertIndex) {
    //     targetParent.splice(insertIndex + 1, 0, temp)
    //   }

    // }
    // 


    // targetParent.splice();
  }
  console.log(newArray);
  return newArray
}
export function pushArrayBy(array, targetValue, key, item, addIndex = 0) {
  let newArray = [...array]

  let targetParent = null;
  let targetIndex = null;
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      if (item[key] == targetValue) {
        targetParent = arr;
        targetIndex = i;
        break;
      }
      if (targetParent) {
        break;
      }
      // if ( !targetParent) {
      if (Array.isArray(arr[i].children)) {
        findItem(arr[i].children);
      }
      // }
      // if (sourceParent && targetParent) {
      //   break;
      // }
    }
  }
  findItem(newArray)
  if (targetParent) {

    let index = targetIndex + addIndex
    if (index == -1) {
      targetParent.unshift(item)
    } else {
      targetParent.splice(targetIndex + addIndex + 1, 0, item)
    }

  }
  if (targetParent.length > 1) {

    const index = targetParent.findIndex(v => v.component === 'Placeholder')
    if (index > -1) {
      targetParent.splice(index, 1)
    }
  }
  return newArray;
}
export function findArrayBy(array: any[], targetValue: any, key: string) {
  let item2
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      if (item[key] == targetValue) {
        item2 = arr[i];
        break;
      }
      if (item2) {
        break;
      }

      if (Array.isArray(arr[i].children)) {
        findItem(arr[i].children);
      }

    }
  }
  findItem(array)
  return item2
}
export function replaceArrayField(array: any[], key: string, callback: Function) {
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      console.log('itemitemitem',i,item)
      //  if (item[key]) {
        item[key] = callback(item)

      // }

      // if ( !targetParent) {
      if (Array.isArray(arr[i].children)) {
        findItem(arr[i].children);
      }

    }
  }
  findItem(array)
  return array
}
export function deleteArrayBy(array: any[], targetValue: any, key: string) {
  let newArray = [...array]

  let targetParent = null;
  let targetIndex = null;
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      if (item[key] == targetValue) {
        targetParent = arr;
        targetIndex = i;
        break;
      }
      if (targetParent) {
        break;
      }
      // if ( !targetParent) {
      if (Array.isArray(arr[i].children)) {
        findItem(arr[i].children);
      }
      // }
      // if (sourceParent && targetParent) {
      //   break;
      // }
    }
  }
  findItem(newArray)
  if (targetParent) {


    targetParent.splice(targetIndex, 1)


  }
  return newArray;
}

export function isEmptyArray(arr: any) {
  return Array.isArray(arr) ? arr.length == 0 : true
}